//
//  JBPickerView.swift
//  joyBao
//
//  Created by Chen on 2017/7/27.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class JBPickerView: UIView {
    
    fileprivate var dataArray = [String]()
    fileprivate var selectCallback: ((_ title: String) -> ())?
    fileprivate var dismissCallback: (() -> Swift.Void)?
    fileprivate var tapGesture: UITapGestureRecognizer?
    fileprivate var containerView: UIView?
    fileprivate var toolBar: UIView?
    fileprivate var pickerView: UIPickerView?
    fileprivate let kToolBarHeight = 45
    fileprivate let kPickerViewHeight = 250

    func showPickerView(_ dataArray: Array<String>, selectRowTitle: String, selectCallback: ((_ title: String) -> ())?, dismissCallback: (() -> Swift.Void)?) {
        self.dataArray = dataArray
        self.selectCallback = selectCallback
        self.dismissCallback = dismissCallback
        
        self.setupUI()
        
        if let index = dataArray.index(where: { $0 == selectRowTitle }) {
            self.pickerView?.selectRow(index, inComponent: 0, animated: false)
        }
        
        self.showPickerView()
    }
    
    func setupUI() {
        weak var weakSelf = self
        
        self.tapGesture = UITapGestureRecognizer()
        self.tapGesture?.rx.event.subscribe({ (event: Event) in
            switch event {
            case .next(let tapGesture):
                let y = tapGesture.location(in: self).y
                if y < (weakSelf?.containerView?.top)! {
                    weakSelf?.dismissPickerView()
                }
            default: break
            }
        }).addDisposableTo(bag)
        self.addGestureRecognizer(self.tapGesture!)
        
        self.containerView = UIView(frame: CGRect(x: 0, y: 0, width: Int(self.width), height: kToolBarHeight + kPickerViewHeight))
        self.containerView?.top = ScreenHeight
        self.containerView?.backgroundColor = UIColor.white
        self.addSubview(self.containerView!)
        
        self.toolBar = UIView(frame: CGRect(x: 0, y: 0, width: Int((self.containerView?.width)!), height: kToolBarHeight))
        self.toolBar?.backgroundColor = UIColor.white
        self.containerView?.addSubview(self.toolBar!)
        
        let cancelBtn = UIButton(type: .system)
        cancelBtn.frame = CGRect(x: 10, y: 0, width: kToolBarHeight, height: kToolBarHeight)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(Normal_Text_Color, for: .normal)
        cancelBtn.titleLabel?.font = Font(font: 16)
        cancelBtn.touchUpInsideCallback {
            weakSelf?.dismissPickerView()
        }
        self.toolBar?.addSubview(cancelBtn)
        
        let sureBtn = UIButton(type: .system)
        sureBtn.frame = CGRect(x: 0, y: 0, width: kToolBarHeight, height: kToolBarHeight)
        sureBtn.right = (self.toolBar?.width)! - 10
        sureBtn.setTitle("确定", for: .normal)
        sureBtn.setTitleColor(Nav_BG_Color, for: .normal)
        sureBtn.titleLabel?.font = Font(font: 16)
        sureBtn.touchUpInsideCallback {
            let index = weakSelf?.pickerView?.selectedRow(inComponent: 0)
            weakSelf?.selectCallback?((weakSelf?.dataArray[index!])!)
            weakSelf?.dismissPickerView()
        }
        self.toolBar?.addSubview(sureBtn)
        
        let line = UIView(frame: CGRect(x: 0, y: CGFloat(kToolBarHeight) - OnePixelLineWidth, width: (self.toolBar?.width)!, height: OnePixelLineWidth))
        line.backgroundColor = App_Line_Color
        self.toolBar?.addSubview(line)
        
        self.pickerView = UIPickerView(frame: CGRect(x: 0, y: kToolBarHeight, width: Int(ScreenWidth), height: kPickerViewHeight))
        self.pickerView?.showsSelectionIndicator = true
        self.pickerView?.delegate = self
        self.pickerView?.dataSource = self
        self.containerView?.addSubview(self.pickerView!)
    }
    
    func showPickerView() {
        UIView.animate(withDuration: 0.25) { 
            self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
            self.containerView?.top = self.height - (self.containerView?.height)!
        }
    }
    
    func dismissPickerView() {
        UIView.animate(withDuration: 0.25, animations: { 
            self.backgroundColor = UIColor.clear
            self.containerView?.top = self.height
            self.removeGestureRecognizer(self.tapGesture!)
        }) { (finished) in
            self.dismissCallback?()
        }
    }
}

extension JBPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return self.dataArray[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 30
    }
}
